<?php
/*
 * @开发工具: JetBrains PhpStorm.
 * @文件名：Credit.class.php
 * @类功能: 信用卡
 * @开发者: 陈旭林
 * @开发时间： 2017-09-01
 * @版本：version 1.0
 */

namespace app\credit\controller;

use app\common\cache\bank\IBankCache;
use app\common\cache\credit\ICreditCache;
use app\common\cache\user\IUserCache;
use app\common\controller\CreditBaseController;
use app\common\enum\IsDelete;
use app\common\enum\IsTrack;
use app\common\ext\IDb;
use app\common\ext\ILog;

class Credit extends CreditBaseController {

    /**
     * @功能：信用卡申请列表
     * @开发者：cxl
     */
    public function credit_apply_list(){
        //接收参数
        $bottom_id  = self::get_data('bottom_id');

        //查询记录条数
        $credit_apply_where['ct.is_delete'] = IsDelete::No;
        $credit_apply_where['bk.is_delete'] = IsDelete::No;
        $credit_apply_where['cal.agent_admin_id'] = self::$uid;
        if(!empty($bottom_id)) $credit_apply_where['cal.credit_apply_id'] = array("lt",$bottom_id);
        $credit_apply_list = IDb::getInstance("credit_apply as cal")
            ->setDbFiled("cal.credit_apply_id,cal.user_id,bk.bank_id,bk.bank_name,ct.credit_product_id,ct.credit_product_name,cal.credit_apply_time")
            ->setDbJoin("credit_product as ct","cal.credit_product_id=ct.credit_product_id")
            ->setDbJoin("bank as bk","bk.bank_id=ct.bank_id")
            ->setDbWhere($credit_apply_where)
            ->setDbOrder("cal.credit_apply_id desc")
            ->setDbLimit(10)
            ->sel();

        //判断列表是否存在
        if($credit_apply_list === false){
            self::set_code(self::WARNING_0001);
            self::set_msg("取得信用卡申请列表失败！");
            self::send();
        }

        //整理银行图片信息
        if(empty($credit_apply_list) || !is_array($credit_apply_list)) {
            $credit_apply_list = array();
        }

        //整理数据
        foreach($credit_apply_list as &$data){
            //设置查询条件
            $product_info = ICreditCache::getInfo($data['credit_product_id']);
            if(empty($product_info['credit_product_id'])){
                self::set_code(self::WARNING_0002);
                self::set_msg("取得贷款申请记录失败！");
                self::send();
            }

            //设置贷款机构名称
//            $data['credit_product_name'] = "{$product_info['loan_agency_name']}-{$product_info['credit_product_name']}";
            $bank_info = IBankCache::getInfo($data['bank_id'],['bank_name','bank_url']);
            $data['bank_name'] = $bank_info['bank_name'];
            $data['bank_url'] = $bank_info['bank_url'];
            $data['credit_product_name'] = $product_info['credit_product_name'];

            $data['credit_apply_time'] = substr($data['credit_apply_time'],0,16);

            $user_info = IUserCache::getInfo($data['user_id'],['real','phone']);
            $data['real'] = $user_info['real'];
            $data['loan_phone'] = $user_info['phone'];

            $data['not_log_day'] = 0;

            $log_where['credit_apply_id'] = $data['credit_apply_id'];
            $log_where['is_track'] = IsTrack::Yes;
            $log_info = IDb::getInstance("credit_apply_log")
                ->setDbFiled("operate_time")
                ->setDbWhere($log_where)
                ->setDbOrder('credit_apply_log_id desc')
                ->row();
            $time = strtotime($log_info['operate_time']);

            if(!empty($log_info) && $time < time()){
                $diff = intval((time()-$time)/3600/24);
                if($diff >= 3){
                    $data['not_log_day'] = $diff;
                }
            }
        }

        //返回值
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("credit_apply_log_list",$credit_apply_list);
        self::send();
    }

    /**
     * @功能：信用卡产品进度查询日志
     * @开发者：cxl
     */
    public function credit_apply_log(){
        //接收参数
        $apply_id  = self::get_data('credit_apply_id',self::NOT_EMPTY,"贷款产品申请编码不能为空！");
        $bottom_id  = self::get_data('bottom_id');

        //设置查询条件
        $loan_apply_log_where['credit_apply_id'] = $apply_id;
        $loan_apply_log_where['is_track'] = IsTrack::Yes;
        if(!empty($bottom_id)) $loan_apply_log_where['credit_apply_log_id'] = array("lt",$bottom_id);

        $apply_log_list = IDb::getInstance("credit_apply_log")
            ->setDbFiled("credit_apply_log_id,operate_note,operate_time")
            ->setDbWhere($loan_apply_log_where)
            ->setDbOrder("credit_apply_log_id desc")
            ->setDbLimit(10)
            ->sel();

        foreach ($apply_log_list as $key=>$value){
            $apply_log_list[$key]['video_list'] = array();
            $apply_log_list[$key]['audio_list'] = array();
            $apply_log_list[$key]['img_list'] = array();
        }

        //判断列表是否存在
        if($apply_log_list === false){
            self::set_code(self::WARNING_0001);
            self::set_msg("信用卡申请进度查询失败！");
            self::send();
        }

        //返回值
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("credit_apply_log",$apply_log_list);
        self::send();
    }

    /**
     * @功能：信用卡进度添加
     * @开发者：gys
     */
    public function apply_log_add(){
        //接收参数
        $credit_apply_id  = self::get_data('credit_apply_id',self::NOT_EMPTY,"申请编号不能为空！");
        $note  = self::get_data('note',self::NOT_EMPTY,"内容不能为空");

        $where['credit_apply_id'] = $credit_apply_id;
        $where['agent_admin_id'] = self::$uid;
        $apply_info = IDb::getInstance('credit_apply')
            ->setDbWhere($where)
            ->row();
        if(empty($apply_info)){
            self::set_code(self::ERR_0001);
            self::set_msg("申请不存在！");
            self::send();
        }

        $result = ILog::CreditApplyLog($credit_apply_id,null,null,self::$uid,$note,IsTrack::Yes);
        if(!$result){
            self::set_code(self::ERR_0001);
            self::set_msg("添加日常跟踪失败！");
            self::send();
        }

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::send();
    }
}
